#include "client.h"
client::client()
{
    table = NULL;
}
client::~client()
{
	delete table;
}
int client::init()
{
#ifdef _WIN32
    WSADATA wsa;
    if(WSAStartup(0x0202,&wsa) == -1)
    {
        cout<<"Winsock init failed !!"<<endl;
        exit(-1);
    }
    else
    {

#endif

        getparametter();
        int status = createsocket();
        if(status == 0)
        {

            return 0;
        }
        else
        {

            return -1;
        }
#ifdef _WIN32
    }
#endif

}
//lay para can thiet
void client::getparametter()
{
    cout<<endl<<"Server ip : ";
    cin>>this->sv_addr;
    this->port = 9999;
    server.sin_family = AF_INET;
    server.sin_addr.s_addr = inet_addr(sv_addr);
    server.sin_port = htons(port);

}
int client::createsocket()
{
    cl = socket(AF_INET,SOCK_STREAM,IPPROTO_IP);
    if(cl == -1)
    {

        cout<<"Create socket failed: "<<endl;
		cin.get();cin.get();
        exit(2);
        return -1;
    }
    else
    {
        cout<<"Create socket successfull"<<endl;
        return 0;
    }
}
int client::clientconnect()
{
    int con = connect(cl,(sockaddr *)&server,sizeof(server));
    if(con == -1)
    {
        cout<<"Connect server failed :"<<endl;
       cin.get();cin.get();
		finish();
		return -1;

    }
    else
    {
        cout<<"Connected server successfull "<<endl;
		return 1;

    }
}

int client::clientsend()
{
    // sent.printmessage();
    int size = send(cl,(char *)&sent,sizeof(sent),0);
    //cout<<"Client sent : "<<size<<endl;
    return size;

}
void client::clientrecv()
{

    recv(cl,(char *)&buff,sizeof(buff),0);
}
void client::playgame()
{
    
   // buff.setvalue(-1,-1,-1," ");
    int size=0;
    bool flag = false;
    bool fg = false;
    while(1)
    {
		cout<<"Waiting for server... "<<endl;
        clientrecv();
        int type = buff.gettype();
        switch(type)
        {
		case -10:
			cout<<"A client disconected ,game could not continue !!! "<<endl;
			cin.get();cin.get();
			finish();
			exit(1);
			break;
		case 0:
			cout<<"Game finished"<<endl;
			cin.get();cin.get();
			finish();
			exit(1);
			break;
        case 3:
			cout<<"---------------------------------------------------------"<<endl;
			cout<<"\t\tGame start\t\t"<<endl;
			//cout<<string(50,'\n');
            size = buff.getx();
            table = new map(buff.getx());
			table->draw();
            break;
        case -4:
        case 4:
            cout<<string(50,'\n');
            table->draw();
            int a,b;
again:
            cout<<"Please,choose a position on map : "<<endl;
            cout<<"Row : ";
            cin>>a;
            cout<<"Col  :  ";
            cin>>b;
            if(a<=0||a >size||b<=0||b>size)
            {
                goto again;
            }
            else
            {
                sent.setvalue(4,a,b,"POSITION");
                clientsend();
            }
            break;
        case 8:

            if(fg == false)
            {
                cout<<"--------------------Winner -----------------------"<<endl;
                cout<<buff.getmessage()<<endl;
                fg = true;
            }
            else
            {

                cout<<buff.getmessage()<<endl;
            }


            break;

        case 9 :
            if(flag == false)
            {
                cout<<"--------------------Result -----------------------"<<endl;
                cout<<buff.getmessage()<<"  :  "<<buff.getx()<<" points"<<endl;
            }
            else
            {
                cout<<buff.getmessage()<<"  :  "<<buff.getx()<<" points"<<endl;
            }
            flag = true;
            break;
        case 10 :
            string d;
            d = buff.getmessage();
            table->setvalue(d[0],buff.getx(),buff.gety());
            cout<<string(50,'\n');
            table->draw();
            break;

        }
    };
   
    //type = 0 la thoat hoac ket thuc chuong trinh
    //1 = login
    // 2 login thanh cong
    //3 la play game,chon hang cot,-3 la chon lai do cot do da chon roi
}
void client::login()
{
    while(1)
    {
        clientrecv();
        if(buff.gettype() == 2)
        {
            cout<<"Register nickname success, your nickname : "<<user<<endl;
            break;
        }
        else
        {
			cout<<"Register your nickname :"<<endl;
            cout<<"Nickname include <A-Z,a-z,0-9> and more 3 ,less 10 characters."<<endl;
			cout<<"Input nickname :";
            cin>>user;
            sent.setvalue(1,-1,-1,user);
            clientsend();

        }

    }
}
void client::clientintro()
{
    cout<<"=======================LAP TRINH SOCKET=================================="<<endl;
    cout<<"\t\tProgramming with socket API"<<endl;
    cout<<"\t\t@Author :\tPham Van Diep"<<endl;
    cout<<"\t\t@Enviroment : c++ "<<endl;
    cout<<"\t\t@Port : 9999"<<endl<<endl;
    cout<<"\t************************************************************"<<endl;
    cout<<"\t\tRule of game:"<<endl;
    cout<<"\t\tStart,you need to input server ip "<<endl;
    cout<<"\t\tIf connect to server "<<endl;
    cout<<"\t\tYou need to choose a nickname"<<endl;
    cout<<"\t\tRegister nickname successfully,and game start."<<endl;
    cout<<"\t\tClient input num of row and col of map"<<endl;
    cout<<"\t\tServer process to calculate points"<<endl;
    cout<<"\t\tWaiting for your time"<<endl;
    cout<<"\t\tWhen all openned ,game will finished "<<endl;
    cout<<"\t\tServer will sent all points of clients"<<endl;
    cout<<"\t:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::"<<endl;

}
void client::run()
{
    clientintro();

    if(init() == 0)
    {
        if(clientconnect() == 1)
		{
			login();
			playgame();
		}

    }
    finish();
}
void client::finish()
{
    //shutdown(cl,SD_BOTH);
#ifdef _WIN32
    closesocket(cl);
    WSACleanup();
#endif
#ifdef linux
    close(cl);
#endif

}


